package com.ruoyi.zhmk.controller;

import java.util.List;

import com.ruoyi.zhmk.domain.ZhmkPost;
import com.ruoyi.zhmk.domain.ZhmkUser;
import com.ruoyi.zhmk.service.IZhmkUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zhmk.domain.ZhmkAsk;
import com.ruoyi.zhmk.service.IZhmkAskService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人员请假Controller
 * 
 * @author sxl
 * @date 2021-06-15
 */
@Controller
@RequestMapping("/zhmk/ask")
public class ZhmkAskController extends BaseController
{
    private String prefix = "zhmk/ask";

    @Autowired
    private IZhmkAskService zhmkAskService;

    @Autowired
    private IZhmkUserService zhmkUserService;

    @RequiresPermissions("zhmk:ask:view")
    @GetMapping()
    public String ask(ModelMap mmap)
    {
        mmap.put("users",zhmkUserService.selectUserAll());
        return prefix + "/ask";
    }

    /**
     * 首页展示人员请假信息
     * @param mmap
     * @return
     */
    @RequiresPermissions("zhmk:ask:view")
    @GetMapping("/askIndex")
    public String askIndex(ModelMap mmap)
    {
        mmap.put("users",zhmkUserService.selectUserAll());
        return prefix + "/askIndex";
    }

    /**
     * 查询人员请假列表
     */
    @RequiresPermissions("zhmk:ask:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZhmkAsk zhmkAsk)
    {
        startPage();
        List<ZhmkAsk> list = zhmkAskService.selectZhmkAskList(zhmkAsk);
        ZhmkUser user = null;
        for(ZhmkAsk ask : list){
            user = zhmkUserService.selectZhmkUserById(ask.getUserId());
            if(user!=null){
                ask.setUpdateBy(user.getUserName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出人员请假列表
     */
    @RequiresPermissions("zhmk:ask:export")
    @Log(title = "人员请假", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZhmkAsk zhmkAsk)
    {
        List<ZhmkAsk> list = zhmkAskService.selectZhmkAskList(zhmkAsk);
        ExcelUtil<ZhmkAsk> util = new ExcelUtil<ZhmkAsk>(ZhmkAsk.class);
        return util.exportExcel(list, "人员请假数据");
    }

    /**
     * 新增人员请假
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("users",zhmkUserService.selectUserAll());
        return prefix + "/add";
    }

    /**
     * 新增保存人员请假
     */
    @RequiresPermissions("zhmk:ask:add")
    @Log(title = "人员请假", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZhmkAsk zhmkAsk)
    {
        return toAjax(zhmkAskService.insertZhmkAsk(zhmkAsk));
    }

    /**
     * 修改人员请假
     */
    @GetMapping("/edit/{askId}")
    public String edit(@PathVariable("askId") Long askId, ModelMap mmap)
    {
        ZhmkAsk zhmkAsk = zhmkAskService.selectZhmkAskById(askId);
        mmap.put("zhmkAsk", zhmkAsk);
        mmap.put("user", zhmkUserService.selectZhmkUserById(zhmkAsk.getUserId()));//查人员
        return prefix + "/edit";
    }

    /**
     * 修改保存人员请假
     */
    @RequiresPermissions("zhmk:ask:edit")
    @Log(title = "人员请假", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZhmkAsk zhmkAsk)
    {
        return toAjax(zhmkAskService.updateZhmkAsk(zhmkAsk));
    }

    /**
     * 删除人员请假
     */
    @RequiresPermissions("zhmk:ask:remove")
    @Log(title = "人员请假", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zhmkAskService.deleteZhmkAskByIds(ids));
    }
}
